package com.openbravo.pos.util;

import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.awt.print.Book;
import java.awt.print.PageFormat;
import java.awt.print.Paper;
import java.awt.print.Printable;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;
import java.util.List;
import javax.print.PrintService;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JRExporterParameter;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.export.JRGraphics2DExporter;
import net.sf.jasperreports.engine.export.JRGraphics2DExporterParameter;
import net.sf.jasperreports.engine.util.JRGraphEnvInitializer;

public class JRPrinterAWT300
  implements Printable
{
  private JasperPrint jasperPrint = null;
  private int pageOffset = 0;

  protected JRPrinterAWT300(JasperPrint paramJasperPrint)
    throws JRException
  {
    JRGraphEnvInitializer.initializeGraphEnv();
    this.jasperPrint = paramJasperPrint;
  }

  public static boolean printPages(JasperPrint paramJasperPrint, int paramInt1, int paramInt2, PrintService paramPrintService)
    throws JRException
  {
    JRPrinterAWT300 localJRPrinterAWT300 = new JRPrinterAWT300(paramJasperPrint);
    return localJRPrinterAWT300.printPages(paramInt1, paramInt2, paramPrintService);
  }

  public static Image printPageToImage(JasperPrint paramJasperPrint, int paramInt, float paramFloat)
    throws JRException
  {
    JRPrinterAWT300 localJRPrinterAWT300 = new JRPrinterAWT300(paramJasperPrint);
    return localJRPrinterAWT300.printPageToImage(paramInt, paramFloat);
  }

  private boolean printPages(int paramInt1, int paramInt2, PrintService paramPrintService)
    throws JRException
  {
    int i = 1;
    if ((paramInt1 < 0) || (paramInt1 > paramInt2) || (paramInt2 >= this.jasperPrint.getPages().size()))
      throw new JRException("Invalid page index range : " + paramInt1 + " - " + paramInt2 + " of " + this.jasperPrint.getPages().size());
    this.pageOffset = paramInt1;
    PrinterJob localPrinterJob = PrinterJob.getPrinterJob();
    initPrinterJobFields(localPrinterJob);
    PageFormat localPageFormat = localPrinterJob.defaultPage();
    Paper localPaper = localPageFormat.getPaper();
    localPrinterJob.setJobName("JasperReports - " + this.jasperPrint.getName());
    switch (this.jasperPrint.getOrientation())
    {
    case 2:
      localPageFormat.setOrientation(0);
      localPaper.setSize(this.jasperPrint.getPageHeight(), this.jasperPrint.getPageWidth());
      localPaper.setImageableArea(0.0D, 0.0D, this.jasperPrint.getPageHeight(), this.jasperPrint.getPageWidth());
      break;
    case 1:
    default:
      localPageFormat.setOrientation(1);
      localPaper.setSize(this.jasperPrint.getPageWidth(), this.jasperPrint.getPageHeight());
      localPaper.setImageableArea(0.0D, 0.0D, this.jasperPrint.getPageWidth(), this.jasperPrint.getPageHeight());
    }
    localPageFormat.setPaper(localPaper);
    Book localBook = new Book();
    localBook.append(this, localPageFormat, paramInt2 - paramInt1 + 1);
    localPrinterJob.setPageable(localBook);
    try
    {
      if (paramPrintService == null)
      {
        if (localPrinterJob.printDialog())
          localPrinterJob.print();
      }
      else
      {
        localPrinterJob.setPrintService(paramPrintService);
        localPrinterJob.print();
      }
    }
    catch (Exception localException)
    {
      throw new JRException("Error printing report.", localException);
    }
    return i==1; //ati
  }

  public int print(Graphics paramGraphics, PageFormat paramPageFormat, int paramInt)
    throws PrinterException
  {
    if (Thread.currentThread().isInterrupted())
      throw new PrinterException("Current thread interrupted.");
    paramInt += this.pageOffset;
    if ((paramInt < 0) || (paramInt >= this.jasperPrint.getPages().size()))
      return 1;
    try
    {
      JRGraphics2DExporter localJRGraphics2DExporter = new JRGraphics2DExporter();
      localJRGraphics2DExporter.setParameter(JRExporterParameter.JASPER_PRINT, this.jasperPrint);
      localJRGraphics2DExporter.setParameter(JRGraphics2DExporterParameter.GRAPHICS_2D, paramGraphics);
      localJRGraphics2DExporter.setParameter(JRExporterParameter.PAGE_INDEX, new Integer(paramInt));
      localJRGraphics2DExporter.exportReport();
    }
    catch (JRException localJRException)
    {
      localJRException.printStackTrace();
      throw new PrinterException(localJRException.getMessage());
    }
    return 0;
  }

  private Image printPageToImage(int paramInt, float paramFloat)
    throws JRException
  {
    BufferedImage localBufferedImage = new BufferedImage((int)(this.jasperPrint.getPageWidth() * paramFloat) + 1, (int)(this.jasperPrint.getPageHeight() * paramFloat) + 1, 1);
    JRGraphics2DExporter localJRGraphics2DExporter = new JRGraphics2DExporter();
    localJRGraphics2DExporter.setParameter(JRExporterParameter.JASPER_PRINT, this.jasperPrint);
    localJRGraphics2DExporter.setParameter(JRGraphics2DExporterParameter.GRAPHICS_2D, localBufferedImage.getGraphics());
    localJRGraphics2DExporter.setParameter(JRExporterParameter.PAGE_INDEX, new Integer(paramInt));
    localJRGraphics2DExporter.setParameter(JRGraphics2DExporterParameter.ZOOM_RATIO, new Float(paramFloat));
    localJRGraphics2DExporter.exportReport();
    return localBufferedImage;
  }

  public static void initPrinterJobFields(PrinterJob paramPrinterJob)
  {
    try
    {
      paramPrinterJob.setPrintService(paramPrinterJob.getPrintService());
    }
    catch (PrinterException localPrinterException)
    {
    }
  }

  public static long getImageSize(JasperPrint paramJasperPrint, float paramFloat)
  {
    int i = (int)(paramJasperPrint.getPageWidth() * paramFloat) + 1;
    int j = (int)(paramJasperPrint.getPageHeight() * paramFloat) + 1;
    return i * j;
  }
}

/* Location:           D:\PowerFulPOS-2.30.2\openbravopos.jar
 * Qualified Name:     com.openbravo.pos.util.JRPrinterAWT300
 * JD-Core Version:    0.6.0
 */